<?php

namespace app\admin\controller;

use think\Exception;
use think\facade\View;
use app\admin\business\Category as CategoryBus;

class Category extends AdminBase
{
    public function index()
    {
        $pid = input("param.pid", "", "intval");

        $data = [
            'pid' => $pid,
        ];

        try {
            $categorys = (new \app\admin\business\Category)->getList($data, 5);
        } catch (\Exception $e) {
            $categorys = [];
        }

//        return json($categorys);

        return View::fetch("",
            ['categorys' => $categorys,
                'pid' => $pid
            ]);
    }

    public function test()
    {
        $pid = input("param.pid", "", "intval");
        $data = [
            'pid' => $pid,
        ];
        $categorys = (new \app\admin\business\Category)->test($data);


        return json($categorys);
    }

    //添加分类界面
    public function add()
    {
        try {
            $categorys = (new \app\admin\business\Category)->getNormalCategorys();
        } catch (\Exception $e) {
            $categorys = [];
        }
        return View::fetch(
            "",
            ['categorys' => json_encode($categorys),]
        );
    }

    //编辑分类
    public function edit()
    {

    }

    // 添加分类保存
    public function save()
    {
        $pid = input("param.pid", '', "trim");
        $name = input("param.name", '', 'trim');
        $data = [
            'pid' => $pid,
            'name' => $name,
            'status' => config("status.mysql.table_normal")
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('add_category')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        try {
            $result = (new \app\admin\business\Category)->add($data);
        } catch (\Exception $e) {
            return show(config("status.error"), $e->getMessage());
        }
        if ($result) {
            return show(config("status.success"), "添加分类成功");
        }
        return show(config("status.error"), "添加分类失败");

    }

    // 分类管理排序
    public function listorder()
    {
        $listorder = input("param.listorder",0, "intval");
        $id = input("param.id", 0, "intval");

//        $param = input("param.");
//        dump($param);
        if (!$id) {
            return show(config("status.error"), "参数错误");
        }
        try {
            $result = (new \app\admin\business\Category)->getListorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config("status.error"), $e->getMessage());
        }
        if ($result) {
            return show(config("status.success"), "排序成功");
        } else {
            return show(config("status.error"), "排序失败");
        }

    }

    // 分类管理状态更改
    public function status()
    {
        $id = input("param.id", '', 'intval');
        $status = input("param.status", '', 'intval');
        $data = [
            'id' => $id,
            'status' => $status
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('status')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        try {
            $result = (new \app\admin\business\Category)->getStatus($data['id'], $data['status']);
        } catch (\Exception $e) {
            return show(config("status.error"), $e->getMessage());
        }
        if ($result) {
            return show(config("status.success"), "更新状态成功");
        } else {
            return show(config("status.error"), "更新状态失败");
        }

    }

    public function dialog()
    {
        $categorys = (new CategoryBus())->getNormalByPid();

        return view( "",
            ['categorys' => json_encode($categorys),]);
    }

    public function getByPid()
    {
        $pid = input("param.pid", '', 'intval');
        $categorys = (new CategoryBus())->getNormalByPid($pid);
        return show(config("status.success"), "OK", $categorys);
    }
}